package com.shizhanzhe.gxb.my.activity;

import android.annotation.SuppressLint;
import android.os.Bundle;
import android.os.CountDownTimer;
import android.util.Log;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.gyf.barlibrary.ImmersionBar;
import com.shizhanzhe.gxb.R;
import com.shizhanzhe.gxb.base.UserBean;
import com.shizhanzhe.gxb.url.Path;
import com.shizhanzhe.gxb.utils.OkHttpDownloadJsonUtil;
import com.shizhanzhe.gxb.utils.Util;

import org.json.JSONException;
import org.json.JSONObject;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import me.imid.swipebacklayout.lib.app.SwipeBackActivity;

/**
 * @author Administrator
 */
public class ChangePsdActivity extends SwipeBackActivity {
    @BindView(R.id.top_back)
    ImageButton topBack;
    @BindView(R.id.top_title)
    TextView topTitle;
    @BindView(R.id.et_forget_pwd)
    EditText etForgetPwd;
    @BindView(R.id.et_forget_confirmpwd)
    EditText etForgetConfirmpwd;
    @BindView(R.id.et_forget_yanzheng)
    EditText etForgetYanzheng;
    @BindView(R.id.tv_forget_yuyin)
    TextView tvForgetYuyin;
    @BindView(R.id.tv_forget_commit)
    TextView tvForgetCommit;
    @BindView(R.id.ll_phone_unbind)
    LinearLayout llPhoneUnbind;
    private CountDownTimer time;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_change_pwd);
        ButterKnife.bind(this);
        ImmersionBar.with(this)
                .fitsSystemWindows(true)  //使用该属性,必须指定状态栏颜色
                .statusBarColor(R.color.white)
                .statusBarDarkFont(true, 0.2f)
                .init();
        topTitle.setText("修改密码");
    }

    @OnClick({R.id.top_back, R.id.tv_forget_commit, R.id.tv_forget_yuyin})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.top_back:
                finish();
                break;
            case R.id.tv_forget_commit:
                if (check(etForgetPwd.getText().toString(), etForgetConfirmpwd.getText().toString(), etForgetYanzheng.getText().toString())) {
                    //修改密码接口
                    changeps(UserBean.mobile, etForgetYanzheng.getText().toString(), etForgetPwd.getText().toString());
                }
                break;
            case R.id.tv_forget_yuyin:
                reCode(UserBean.mobile);
                time = new CountDownTimer(60000, 1000) {
                    @SuppressLint("SetTextI18n")
                    @Override
                    public void onTick(long millisUntilFinished) {
                        tvForgetYuyin.setClickable(false);
                        tvForgetYuyin.setText(millisUntilFinished / 1000 + "秒");
                    }

                    @Override
                    public void onFinish() {
                        tvForgetYuyin.setText("重新验证");
                        tvForgetYuyin.setClickable(true);
                    }
                };
                time.start();
                break;
            default:
        }
    }

    private boolean check(String ps, String newPs, String ver) {
        if (!"".equals(ps)) {
            if (ps.length() >= 6 && ps.length() <= 15) {
                if (!"".equals(newPs)) {
                    if (ps.equals(newPs)) {
                        if (!"".equals(ver)) {
                            return true;
                        } else {
                            Toast.makeText(this, "请输入验证码", Toast.LENGTH_SHORT).show();
                        }
                    } else {
                        Toast.makeText(this, "两次密码输入不一致", Toast.LENGTH_SHORT).show();
                    }
                } else {
                    Toast.makeText(this, "请再次输入新密码", Toast.LENGTH_SHORT).show();
                }
            } else {
                Toast.makeText(this, "请输入正确的密码位数", Toast.LENGTH_SHORT).show();
            }
        } else {
            Toast.makeText(this, "请输入新密码", Toast.LENGTH_SHORT).show();
        }
        return false;
    }

    private void reCode(String phone) {
        String sign = Util.encode(Util.encode(phone + "zxbbuy") + "zxbbuy");
        OkHttpDownloadJsonUtil.downloadJson(this, Path.SENDPHONECODE(phone, 0, sign),
                new OkHttpDownloadJsonUtil.onOkHttpDownloadListener() {
                    @Override
                    public void onsendJson(String json) {
                        Log.d("____cps", json);
                        if (!"1".equals(json) && !"2".equals(json)) {
                            try {
                                JSONObject jsonObject = new JSONObject(json);
                                if (jsonObject.has("info")) {
                                    Toast.makeText(ChangePsdActivity.this, jsonObject.getString("info"), Toast.LENGTH_SHORT).show();
                                }
                            } catch (JSONException e) {
                                e.printStackTrace();
                            }
                        } else {
                            Toast.makeText(ChangePsdActivity.this, "网络异常", Toast.LENGTH_SHORT).show();
                        }
                    }
                });
    }

    //修改密码接口
    private void changeps(String phone, String code, String password) {
        OkHttpDownloadJsonUtil.downloadJson(this, Path.password_xiu(phone,code,password),
                new OkHttpDownloadJsonUtil.onOkHttpDownloadListener() {
                    @Override
                    public void onsendJson(String json) {
                        Log.d("____", json);
                        if (!"1".equals(json) && !"2".equals(json)) {
                            try {
                                JSONObject jsonObject = new JSONObject(json);
                                if (jsonObject.has("info")) {
                                    Toast.makeText(ChangePsdActivity.this, jsonObject.getString("info"), Toast.LENGTH_SHORT).show();
                                }
                                if (jsonObject.has("status")) {
                                    if (jsonObject.getInt("status") == 1) {
                                        finish();
                                    }
                                }
                            } catch (JSONException e) {
                                e.printStackTrace();
                            }
                        } else {
                            Toast.makeText(ChangePsdActivity.this, "网络异常", Toast.LENGTH_SHORT).show();
                        }
                    }
                });
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        ImmersionBar.with(this).destroy();
    }
}
